package com.jinjiafeng.baselibrary.http;

import android.content.Context;
import android.support.annotation.NonNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * date:on 18-4-23
 *
 * @author jinjiafeng
 */

public class HttpUtils {

    private static final String JOINT = "?";
    private static final int METHOD_GET = 0;
    private static final int METHOD_POST = 1;
    private static IHttpEngine HTTP_ENGINE ;
    private final Context mContext;
    private final WeakHashMap<String, Object> mParams;
    private String mUrl;
    private int mRequestType;
    private ICallback mICallback;

    /**
     * 是否添加缓存
     */

    private boolean mCache = false;

    private HttpUtils(Context context) {
        mContext = context;
        mParams = new WeakHashMap<>();
    }

    public static HttpUtils with(Context context) {
        return new HttpUtils(context);
    }

    @NonNull
    public static String jointParams(String url, WeakHashMap<String, Object> params) {
        StringBuilder sb = new StringBuilder(url);
        if (!url.contains(JOINT)) {
            sb.append(JOINT);
        } else {
            if (!url.endsWith(JOINT)) {
                sb.append("&");
            }
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        sb.deleteCharAt(sb.length() - 1);
        url = sb.toString();
        return url;
    }

    public HttpUtils cache(boolean isCache){
        this.mCache = isCache;
        return this;
    }

    /**
     * 解析一个类上面的Class信息
     */
    public static Class<?> analysisClazzInfo(Object object) {
        final Type genType = object.getClass().getGenericSuperclass();
        final Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        return (Class<?>) params[0];
    }

    /**
     * 替换网络引擎,可在application类中初始化
     */
    public static void init(IHttpEngine httpEngine) {
        HTTP_ENGINE = httpEngine;
    }

    public HttpUtils url(String url) {
        mUrl = url;
        return this;
    }

    public HttpUtils addParam(String key, String value) {
        mParams.put(key, value);
        return this;
    }

    /**
     * 替换网络引擎
     */
    public HttpUtils exchangeEngine(IHttpEngine httpEngine) {
        HTTP_ENGINE = httpEngine;
        return this;
    }

    public HttpUtils addParams(WeakHashMap<String, Object> params) {
        mParams.putAll(params);
        return this;
    }


    public void get(ICallback callback) {
        mICallback = callback;
        mRequestType = METHOD_GET;
        execute();
    }

    private void execute() {
        if (mICallback == null) {
            mICallback = IHttpEngine.DEFAULT_CALLBACK;
        }

        mICallback.onPreExecute(mParams);

        if (mRequestType == METHOD_GET) {
            HTTP_ENGINE.get(mCache,mContext, mUrl, mParams, mICallback);
        }
        if (mRequestType == METHOD_POST) {
            HTTP_ENGINE.post(mCache,mContext, mUrl, mParams, mICallback);
        }
    }

    public void post(ICallback callback) {
        mICallback = callback;
        mRequestType = METHOD_POST;
        execute();
    }

}
